<?php

namespace App\Http\Traits\Auth;

use App\Models\AbstractUser;
use Illuminate\Http\Request;
use App\Http\Requests\Auth\LoginRequest;
use App\Http\Requests\Auth\SmsLoginRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Auth\ThrottlesLogins;


trait LoginTrait
{
    use  ThrottlesLogins;

    /**
     * 使用账户密码登录
     *
     * @param LoginRequest $request
     *
     * @throws ValidationException
     */
    protected function loginWithCredentials(LoginRequest $request)
    {
        if ($this->hasTooManyLoginAttempts($request)) {

            $this->fireLockoutEvent($request);

            // 登录失败次数太多
            $this->sendLockoutResponse($request);
        }

        $remember = $request->filled('remember');

        if ($this->guard()->attempt($this->credentials($request), $remember
        )) {

            // 登录成功
            // 生成session
            $request->session()->regenerate();

            // 清除登录失败计数
            $this->clearLoginAttempts($request);

            $this->authenticated($request, $this->user());
        } else {

            // 登录失败, 记录错误次数
            $this->incrementLoginAttempts($request);

            // 响应登录失败
            throw ValidationException::withMessages([
                $this->username() => [trans('auth.failed')],
            ]);
        }
    }

    /**
     * 使用短信验证码登录
     *
     * @param SmsLoginRequest $request
     */
    protected function loginWithSms(SmsLoginRequest $request)
    {
        $remember = $request->filled('remember');

        $this->guard()->login($request->user, $remember);

        $this->authenticated($request, $request->user);
    }

    /**
     * 从请求中获取授权凭证
     *
     * @param LoginRequest $request
     *
     * @return array
     * @throws ValidationException
     */
    protected function credentials(LoginRequest $request)
    {
        $username = $request->get($this->username());

        $credentials = $request->only(['password']);

        switch (true) {
            // 如果符合手机号格式,就用手机号登录
            case is_mobile($username):

                $credentials['mobile'] = $username;
                break;
            // 如果符合用户名格式,就用用户名登录
            case is_userName($username):

                $credentials['name'] = $username;
                break;
            // 如果符合邮箱格式,就用邮箱登录
            case is_email($username):

                $credentials['email'] = $username;
                break;
            // 账号格式不正确
            default:
                throw ValidationException::withMessages([
                    $this->username() => [trans('auth.invalid_login')]
                ]);
        }

        return $credentials;
    }

    /**
     * 表单账号字段名
     *
     * @return string
     */
    protected function username()
    {
        return 'login';
    }

    /**
     * 登录完成
     *
     * @param Request $request
     * @param AbstractUser $user
     */
    protected function authenticated(Request $request, AbstractUser $user)
    {
        //
    }

    /**
     * 返回Guard
     *
     * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|mixed
     */
    protected function guard()
    {
        return Auth::guard();
    }
}